package com.lee.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.Result;
import com.lee.base.BaseController;
import com.lee.constant.BusinessType;
import com.lee.system.api.IMenuService;
import com.lee.system.entity.Menu;
import com.lee.system.entity.Role;
import com.lee.utils.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("菜单信息")
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {
	private String prefix = "system/menu";
	@Reference
	private IMenuService menuService;

	@RequiresPermissions("system:menu:view")
	@GetMapping()
	public String menu() {
		return prefix + "/menu";
	}

	@ApiOperation("查询所有菜单")
	@GetMapping("/list")
	@ResponseBody
	public List<Menu> list() {
		return menuService.selectMenuAll();
	}

	/**
	 * 新增菜单
	 * 
	 * @param parentId
	 * @param map
	 * @return
	 */
	@Log(title = "菜单管理", action = BusinessType.INSERT)
	@RequiresPermissions("system:menu:add")
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, Map<String, Object> map) {
		Menu menu = null;
		if (0L != parentId) {
			menu = menuService.selectMenuById(parentId);
		} else {
			menu = new Menu();
			menu.setMenuId(0L);
			menu.setMenuName("主目录");
		}
		map.put("menu", menu);
		return prefix + "/add";
	}

	/**
	 * 更新菜单
	 * 
	 * @return
	 */
	@Log(title = "菜单管理", action = BusinessType.UPDATE)
	@RequiresPermissions("system:menu:edit")
	@GetMapping("/edit/{menuId}")
	public String edit(@PathVariable("menuId") Long menuId, Map<String, Object> map) {
		map.put("menu", menuService.selectMenuById(menuId));
		return prefix + "/edit";
	}

	/**
	 * 保存菜单
	 * 
	 * @param menu
	 * @return
	 */
	@Log(title = "菜单管理", action = BusinessType.SAVE)
	@ApiOperation("保存菜单")
	@PostMapping("/save")
	@ResponseBody
	public Result save(Menu menu) {
		menu.setUpdateBy(ShiroUtils.getLoginName());
		if (menuService.saveMenu(menu) > 0) {
			return Result.ok();
		}
		return Result.error("保存菜单失败");
	}

	/**
	 * 删除菜单
	 * 
	 * @param menuId
	 * @return
	 */
	@ApiOperation("删除菜单")
	@Log(title = "菜单管理", action = BusinessType.DELETE)
	@PostMapping("/remove/{menuId}")
	@ResponseBody
	public Result remove(@PathVariable("menuId") Long menuId) {
		if (menuService.selectCountMenuByParentId(menuId) > 0) {
			return Result.error("存在子菜单，不允许删除");
		}
		if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
			return Result.error("菜单已分配，不允许删除");
		}
		if (menuService.deleteMenuById(menuId) > 0) {
			return Result.ok("删除成功");
		}
		return Result.error();
	}

	/**
	 * 加载角色菜单列表树
	 */
	@ApiOperation("角色菜单树")
	@GetMapping("/roleMenuTreeData")
	@ResponseBody
	public Result roleMenuTreeData(Role role) {
		List<Map<String, Object>> roleMenuTree = menuService.roleMenuTreeData(role);
		return Result.ok().put("data", roleMenuTree);
	}

	/**
	 * 加载所有菜单列表树
	 */
	@ApiOperation("所有菜单树")
	@ResponseBody
	@GetMapping("/menuTreeData")
	public Result menuTreeData(Role role) {
		List<Map<String, Object>> tree = menuService.menuTreeData();
		return Result.ok().put("data", tree);
	}

	/**
	 * 选择菜单树
	 * 
	 * @param menuId
	 * @param map
	 * @return
	 */
	@ApiOperation("选择菜单树")
	@GetMapping("/selectMenuTree/{menuId}")
	public String selectMenuTree(@PathVariable("menuId") Long menuId, Map<String, Object> map) {
		map.put("treeName", menuService.selectMenuById(menuId).getMenuName());
		return prefix + "/tree";
	}

	/**
	 * 选择菜单图标
	 */
	@GetMapping("/icon")
	public String icon() {
		return prefix + "/icon";
	}

	/**
	 * 校验菜单名唯一
	 * 
	 * @return
	 */
	@ApiOperation("校验菜单名唯一")
	@PostMapping("/checkMenuNameUnique")
	@ResponseBody
	public Result checkMenuNameUnique(Menu menu) {
		if (menuService.checkMenuNameUnique(menu)) {
			return Result.ok();
		}
		return Result.error();
	}

}
